package ca.nfb.media.nfbPlayer.ui
{
	import ca.nfb.media.nfbPlayer.ComModule;
	import ca.turbulent.text.SuperText;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.text.TextFormat;
	import flash.utils.Timer;


	public class ToolTip extends MovieClip
	{
		public static const TOOLTIP_STYLE_BOOKMARK	:String = "bookmarksStyle";
		public static const TOOLTIP_STYLE_NORMAL	:String = "normal";
		public static var toolTipTable				:Object = new Object();
		public static var toolTipDelay				:Number = 850;
		
		private var blackArrowHolder_mc				:MovieClip = new MovieClip();
		private var whiteArrowHolder_mc				:MovieClip = new MovieClip();
		
		private var whiteArrow_mc					:ToolTipArrow = new ToolTipArrow();
		private var blackArrow_mc					:BlackArrow = new BlackArrow();
		private var fixedMaxWidth					:Number = 250;
		private var fixedMinHeight					:Number = 80;
		private var maxWidth						:Number = 250;
		private var minHeight						:Number = 45;
		private var targettedDisplayObject			:DisplayObject;
		private var toolTipTimer					:Timer = new Timer(ToolTip.toolTipDelay, 1);
		
		public var whiteArrowText_txt				:SuperText = new SuperText(fixedMaxWidth, fixedMinHeight, new TextFormat(ComModule.getInstance().core.mainFont.fontName, 15, 0, null, null, null, null, null, null, null, null, null, 2));
		public var blackArrowText_txt				:SuperText = new SuperText(maxWidth, minHeight, new TextFormat(ComModule.getInstance().core.mainFont.fontName, 16, 0xFFFFFF));
		
		public var offsetX							:Number = 2;
		public var offsetY							:Number = 2;
		public var toolTipStyle						:String = ToolTip.TOOLTIP_STYLE_NORMAL;
		
		public function ToolTip()
		{
			super();
			initializeToolTip();
			
		}
		
		private function initializeToolTip():void
		{
			toolTipTimer.addEventListener(TimerEvent.TIMER_COMPLETE, displayToolTip, false, 0, true);
			this.visible = false;
			
			addChild(blackArrowHolder_mc);
			addChild(whiteArrowHolder_mc);
			

			whiteArrowText_txt.alignLeft();
			whiteArrowText_txt.autoSize = true;
			whiteArrowText_txt.multiline = true;
			whiteArrowText_txt.wordWrap = true;
			whiteArrowText_txt.interactive = false;
			
			whiteArrowText_txt.autoRedrawBackground = true;
			whiteArrowText_txt.backgroundType = SuperText.BACKGROUND_ADVANCED;
			whiteArrowText_txt.backgroundOutlineAlpha = 1;
			whiteArrowText_txt.hasBackground = true;
			whiteArrowText_txt.backgroundAlpha = 1;
			whiteArrowText_txt.backgroundColor = 0xFFFFFF;
			whiteArrowText_txt.backgroundOutlineColor = 0xFD6400;
			whiteArrowText_txt.backgroundOutlineThickness = 1;
			whiteArrowText_txt.antiAliasType = SuperText.ANTI_ALIAS_ADVANCED;
			whiteArrowText_txt.thickness = 100;
			whiteArrowText_txt.sharpness = -20;
			
			blackArrowText_txt.alignCenter();
			blackArrowText_txt.autoSize = true;
			blackArrowText_txt.multiline = true;
			blackArrowText_txt.wordWrap = true;
			blackArrowText_txt.interactive = false;
			
			blackArrowText_txt.autoRedrawBackground = true;
			blackArrowText_txt.backgroundType = SuperText.BACKGROUND_ADVANCED;
			blackArrowText_txt.backgroundOutlineAlpha = 1;
			blackArrowText_txt.hasBackground = true;
			blackArrowText_txt.backgroundAlpha = 1;
			blackArrowText_txt.backgroundColor = 0x000000;
			blackArrowText_txt.backgroundOutlineColor = 0xFFFFFF;
			blackArrowText_txt.backgroundOutlineThickness = 1;
			blackArrowText_txt.fontColor = 0xFFFFFF;
			blackArrowText_txt.fontSize = 16;
			
			this.filters = [new DropShadowFilter(1, 45, 0, 1, 0, 0, 1)];

			this.mouseEnabled = false;
			this.mouseChildren = false;
			whiteArrowHolder_mc.mouseChildren = false;
			blackArrowHolder_mc.mouseChildren = false;
			
			whiteArrowHolder_mc.addChild(whiteArrowText_txt);
			blackArrowHolder_mc.addChild(blackArrowText_txt);
			blackArrowHolder_mc.addChild(blackArrow_mc);
			whiteArrowHolder_mc.addChild(whiteArrow_mc);
		}
		
		public function displayToolTip(evt:TimerEvent):void
		{
			this.visible = true;
			if (toolTipTimer.running)
				toolTipTimer.stop();
		}
		
		public function drawToolTip():void
		{
			if (this.toolTipStyle == ToolTip.TOOLTIP_STYLE_NORMAL)
			{
				targettedDisplayObject.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved, false, 0, true);
				var point:Point = blackArrowText_txt.localToGlobal(new Point(blackArrowText_txt.x, blackArrowText_txt.y));
				if (this.stage.mouseX <= stage.stageWidth/2)
					blackArrowText_txt.x = -1*blackArrow_mc.width-15;
				else
					blackArrowText_txt.x = -blackArrowText_txt.width+25;
					
				blackArrowText_txt.y = (-1 * blackArrow_mc.height) - blackArrowText_txt.getTextAsset().textHeight - 2.5;
				
			}
			else
			{
				var point2:Point = whiteArrowText_txt.localToGlobal(new Point(whiteArrowText_txt.x, whiteArrowText_txt.y));	
				
				if (this.stage.mouseX <= stage.stageWidth/2)
					whiteArrowText_txt.x = -1*whiteArrow_mc.width-15;
				else
					whiteArrowText_txt.x = -whiteArrowText_txt.width+25;
					
				whiteArrowText_txt.y = (-1 * whiteArrow_mc.height) - whiteArrowText_txt.getTextAsset().textHeight - 3.5;
			}
		}
		
		public function hideToolTip():void
		{
			this.visible = false;
			
			if (targettedDisplayObject)
			{
				if (targettedDisplayObject.hasEventListener(MouseEvent.MOUSE_MOVE))
					targettedDisplayObject.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
			}	
			
			targettedDisplayObject = null;
			if (toolTipTimer.running)
				toolTipTimer.stop();
		}
		
		public function showToolTip(msg:String, targetDisplayObject:DisplayObject=null, delay:Number=-1):void
		{
			whiteArrowHolder_mc.visible = false;
			blackArrowHolder_mc.visible = true;
			
			this.visible = false;
			targettedDisplayObject = targetDisplayObject;
			targettedDisplayObject.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved, false, 0, true);
			this.toolTipStyle = ToolTip.TOOLTIP_STYLE_NORMAL;
			blackArrowText_txt.htmlText = msg.toUpperCase();
			this.x = this.stage.mouseX;
			this.y = this.stage.mouseY;
			
			drawToolTip();
			// blackArrowText_txt.redrawBackground();
			
			if (toolTipTimer.running)
				toolTipTimer.stop();
			
			if (delay > 0)
			{
				toolTipTimer.delay = delay;
			}
			else if (delay == 0)
			{
				toolTipTimer.delay = 1;
			}
			else
			{
				toolTipTimer.delay = ToolTip.toolTipDelay;
			}	
			 	
			toolTipTimer.start();
			
			
		}
		
		private function mouseMoved(evt:MouseEvent=null):void
		{
			this.x = this.stage.mouseX;
			this.y = this.stage.mouseY;
			drawToolTip();
		}
		
		public function showHTMLToolTip(htmlMsg:String, targetDisplayObject:DisplayObject=null, delay:Number=-1):void
		{
			whiteArrowHolder_mc.visible = false;
			blackArrowHolder_mc.visible = true;
			this.visible = false;
			
			targettedDisplayObject = targetDisplayObject;
			targettedDisplayObject.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
			this.toolTipStyle = ToolTip.TOOLTIP_STYLE_NORMAL;
			blackArrowText_txt.getTextAsset().htmlText = htmlMsg.toUpperCase();
			this.x = this.stage.mouseX;
			this.y = this.stage.mouseY;
			drawToolTip();
			// blackArrowText_txt.redrawBackground();
			
			if (toolTipTimer.running)
				toolTipTimer.stop();
			
			if (delay > 0)
			{
				toolTipTimer.delay = delay;
			}
			else if (delay == 0)
			{
				toolTipTimer.delay = 1;
			}
			else
			{
				toolTipTimer.delay = ToolTip.toolTipDelay;
			}	
			 	
			toolTipTimer.start();
			
			
		}
		
		public function showFixedToolTip(prefixMsg:String, htmlMsg:String, point:Point, targetDisplayObject:DisplayObject=null, delay:Number=-1):void
		{
			whiteArrowHolder_mc.visible = true;
			blackArrowHolder_mc.visible = false;
			this.visible = false;
			
			whiteArrowText_txt.getTextAsset().htmlText = prefixMsg + " : " + htmlMsg;
			whiteArrowText_txt.getTextAsset().setTextFormat(new TextFormat(ComModule.getInstance().core.mainFont.fontName, 14, 0x999999, null, null, null, null, null, null, null, null, null, 2), 0, prefixMsg.length+3);
			targettedDisplayObject = targetDisplayObject;
			this.toolTipStyle = ToolTip.TOOLTIP_STYLE_BOOKMARK;
			this.x = point.x;
			this.y = point.y;
			
			drawToolTip();
			whiteArrowText_txt.redrawBackground();
			
			if (toolTipTimer.running)
				toolTipTimer.stop();
			
			if (delay > 0)
			{
				toolTipTimer.delay = delay;
			}
			else if (delay == 0)
			{
				toolTipTimer.delay = 1;
			}
			else
			{
				toolTipTimer.delay = ToolTip.toolTipDelay;
			}	
			 	
			toolTipTimer.start();
		}
		
		public static function addToolTipLabelElement(key:String, englishLabel:String="", frenchLabel:String=""):void
		{
			toolTipTable[key] = {en:englishLabel, fr:frenchLabel};
		}
		
		public static function getLabel(label:String):String
		{
			if (toolTipTable[label])
			{
				return toolTipTable[label][ComModule.getLang().toLowerCase()];
			}
			else
			{
				return "";
			}
		}
		
	}
}